package io.gitee.twoke.generator.engine.impl.persistence.entity.data.impl;

import io.gitee.twoke.generator.engine.impl.AbstractQuery;
import io.gitee.twoke.generator.engine.impl.cfg.GenerateEngineConfigurationImpl;
import io.gitee.twoke.generator.engine.impl.generate.DataTable;
import io.gitee.twoke.generator.engine.impl.generate.impl.DataTableQueryImpl;
import io.gitee.twoke.generator.engine.impl.persistence.DataTableDataManager;
import io.gitee.twoke.generator.engine.impl.persistence.entity.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisDataTableDataManager extends AbstractDataManager<DataTableEntity> implements DataTableDataManager {

    public MybatisDataTableDataManager(GenerateEngineConfigurationImpl generateEngineConfiguration) {
        super(generateEngineConfiguration);
    }

    @Override
    public Class<? extends DataTableEntity> getManagedEntityClass() {
        return DataTableEntityImpl.class;
    }

    @Override
    public DataTableEntity create() {
        return new DataTableEntityImpl();
    }

    @Override
    public long findDataTableCountByQueryCriteria(DataTableQueryImpl dataTableQuery) {
        return (Long)getDbSqlSession().selectOne("selectDataTableCountByQueryCriteria", dataTableQuery);
    }

    @Override
    public List<DataTable> findDataTablesByQueryCriteria(DataTableQueryImpl dataTableQuery) {
        if (dataTableQuery.getResultType().equals(AbstractQuery.ResultType.SINGLE_RESULT)) {
            return getDbSqlSession().selectList("selectDataTableFullByQueryCriteria", dataTableQuery);
        }
        return getDbSqlSession().selectList("selectDataTablesByQueryCriteria", dataTableQuery);
    }

    @Override
    public long findDataTableCountByTableName(String tableName) {
        return (Long)getDbSqlSession().selectOne("selectDataTableCountByTableName", new HashMap<String, Object>(){{
            put("tableName", tableName);
        }});
    }
    @Override
    public DataTable findByTableName(String tableName) {
        return (DataTable) getDbSqlSession().selectOne("selectByTableName", new HashMap<String, Object>(){{
            put("tableName", tableName);
        }});
    }

}